package segger.vasttrafik;

import java.util.TimerTask;

import org.apache.log4j.Logger;

import segger.vasttrafik.io.APIConnection;
import segger.vasttrafik.parsing.LocationParseHandler;

public class Location extends TimerTask {
    private static final Logger logger = Logger.getLogger(Location.class);

    private LocationParseHandler myHandler = new LocationParseHandler();

    private String url;
    private boolean ready = false;

    /* testing purpose */
    Location() {}

    public Location(String searchName) {
        logger.debug("search: " + searchName);
        API.URLBuilder urlBuilder = API.getInstance().getURLBuilder(API.SERVICE_LOCATION_NAME);
        urlBuilder.input(searchName);
        url = urlBuilder.getURL();
    }

    @Override
    public void run() {
        APIConnection connection = new APIConnection();
        connection.httpGet(url, myHandler);
        logger.info("location found");
        ready = true;
    }

    /* testing purpose */
    String getURL() {
        return url;
    }
    
    public String getStopID() {
        return ready ? myHandler.getStopID() : null;
    }
}
